package com.example.samui.case04_16110100414;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class MyAdapter
        extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ArticleBean>list;
    private Context context;
    private LayoutInflater inflater;

    public MyAdapter(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<ArticleBean> list){
        this.list = list;
    }

    class MyViewHolder extends
            RecyclerView.ViewHolder{
        TextView title;
        public MyViewHolder(View itemView){
            super(itemView);
            title = itemView.findViewById(R.id.tv_title);
        }
    }

    @NonNull
    @Override
    public MyViewHolder
    onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item, parent , false);

        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.
            ViewHolder holder, int position) {

        ArticleBean article = list.get(position);
        if (article == null) return;
        MyViewHolder viewHolder = (MyViewHolder) holder;
        viewHolder.title.setText(article.getTitle());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
